import MusicManager from "../../../MusicManager";
import Utils from "../../../Utils";
import Electric from "../../flyer/DestructibleFlyer/FlyerMissile/Electric";
import { Killer } from "../../funcObj/interface/HealthPoint";
import { Location } from "../../info/Predefine";
import GameManager from "../../manager/GameManager";
import NodePoolManager from "../../manager/NodePoolManager";
import Player from "../../player/Player";
import LaunchableFalicity from "../LaunchableFalicity";

const { ccclass, property } = cc._decorator;

/**
 * 电磁炮基地
 */
@ccclass
export default class ElectricBase extends LaunchableFalicity {

    @property(cc.Animation)
    show: cc.Animation = null;

    @property(cc.Node)
    point: cc.Node = null;

    electricCount: number;

    creating: boolean;

    get count(): number {
        return this.electricCount;
    }

    init(player: Player): void {
        super.init(player);
        let clips = this.show.getClips();
        this.show.play(clips[player.playerId].name);
        this.electricCount = 0;
        this.creating = false;
        this.creat();
    }

    creat() {
        if (this.creating) return;
        this.CreatingProgress.progress = 0;
        this.CreatingProgress.node.active = true;
        this.creating = true;
        cc.tween(this.CreatingProgress)
            .to(this.makingTime, { progress: 1 })
            .call(() => {
                this.electricCount++;
                this.player.node.emit(Player.EventType.更新飞行物数量, this.facilityType, 1);
            }).call(() => {
                this.CreatingProgress.node.active = false;
                this.creating = false;
                if (this.electricCount < this.facilityInfo.每基地可制造上限) {
                    this.creat();
                }
            })
            .start();
    }

    dispatch(target: Location) {
        super.dispatch(target);
        this.electricCount--;
        this.player.node.emit(Player.EventType.更新飞行物数量, this.facilityType, -1);
        this.creat();
        let clips = this.show.getClips();
        let flyerParent = this.player.flyerParentNode;
        let electricBullet = NodePoolManager.instance.obtain(NodePoolManager.instance.electricBullet);
        this.dispatchQueue.push({
            flyer: electricBullet, target: target, dispatchFunc: (target: Location) => {
                this.show.play(clips[this.player.playerId + 6].name);
                MusicManager.instance.play(MusicManager.instance.giantLoad, this.node);
                this.show.once(cc.Animation.EventType.FINISHED, () => {
                    MusicManager.instance.play(MusicManager.instance.giantFire, this.node);
                    electricBullet.parent = flyerParent;
                    electricBullet.position = flyerParent.convertToNodeSpaceAR(this.point.convertToWorldSpaceAR(cc.Vec3.ZERO));
                    electricBullet.getComponent(Electric).init(this, target);
                }, this);
            }
        })
    }

    onDispatchSuccess(): void {
        super.onDispatchSuccess();
        if (!this.dispatchQueue.length) {
            let clips = this.show.getClips();
            this.show.node.position = cc.v3(0, -17);
            this.show.play(clips[this.player.playerId].name);
        }
    }

    die(killer: Killer): void {
        super.die(killer);
        this.unscheduleAllCallbacks();
        this.show.stop();
        this.show.targetOff(this)
        this.electricCount = 0;
    }
}
